package scm.inventoryControl.managedbean;

import scm.inventoryControl.session.stateless.InventoryControlSessionBean;
import java.io.Serializable;
import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import mrp.productionPlanning.entity.DailyDetailEntity;
import mrp.productionPlanning.session.DailyDetailSessionBean;
import scm.inventoryControl.entity.RMInventoryLevelEntity;
import scm.rawMaterialPurchasing.entity.BOMLineItemEntity;
import scm.rawMaterialPurchasing.session.BOMSessionBean;

@ManagedBean
@ViewScoped
public class ViewRawMaterialInventoryManagedBean implements Serializable {

    @EJB
    private InventoryControlSessionBean inventorySessionBean;
    @EJB
    private DailyDetailSessionBean dailyDetailSessionBean;
    @EJB
    private BOMSessionBean bomSessionBean;
    
    private RMInventoryLevelEntity selectedInventory;
    
    
    public ViewRawMaterialInventoryManagedBean() {
    }

    public List<RMInventoryLevelEntity> getRMInventories() {
        return inventorySessionBean.getAllRawMaterialInventories();
    }

//      public void viewProcessing(ActionEvent event){
//           setSelectedInventory((ProductInventoryLevelEntity)event.getComponent().getAttributes().get("ViewRecord"));
//           System.out.println("@@@@@@@@@@@@@@@@@@@@@@@"+selectedInventory.getProductBatchRecords().get(0).getQuantity());
//           test = selectedInventory.getProductBatchRecords().get(0).getQuantity();
//           System.out.println("&&&&&&&&&&&&&&&"+test);
//      }
      
  public void everydayDecreaseOfCurrentInventory(){
       Calendar cal = new GregorianCalendar();
        Date date = cal.getTime();
         RMInventoryLevelEntity rmInventory;
        DailyDetailEntity todayEntity = dailyDetailSessionBean.getDailyDetailByDate(date);
        int productAqty= todayEntity.getScheduleA();
       
        int productBqty= todayEntity.getScheduleB();
        int productCqty= todayEntity.getScheduleC();
        int productDqty= todayEntity.getScheduleD();
        int[] productQty = { productAqty, productBqty, productCqty, productDqty };
        System.out.println("@@@@@@@@@@@@@@@"+productBqty);
       for(int i=1;i<5;i++){
           long temp= (long)i;
           Long temp2=Long.valueOf(temp);
           List<BOMLineItemEntity> bomlineitemList=bomSessionBean.getBOMbyId(temp2).getBomLineItems();
           for(int j=0;j<bomlineitemList.size();j++){
               rmInventory=new RMInventoryLevelEntity();
              rmInventory = inventorySessionBean.getInventoryByRMName(bomlineitemList.get(j).getRawMaterial().getRawMaterialName());
              ;
              rmInventory.setCurrentLevel(roundTwoDecimals(rmInventory.getCurrentLevel()-productQty[i-1]*bomlineitemList.get(j).getQuantity()));
              inventorySessionBean.saveChangesOnRMInventory(rmInventory);
           }
            
        }
 
  }
     public double roundTwoDecimals(double d) {
        DecimalFormat twoDForm = new DecimalFormat("#.##");
        return Double.valueOf(twoDForm.format(d));
    }
    
    public RMInventoryLevelEntity getSelectedInventory() {
        return selectedInventory;
    }

    public void setSelectedInventory(RMInventoryLevelEntity selectedInventory) {
        this.selectedInventory = selectedInventory;
    }

 

}
